from django.db import models


#扩展用户表字段
from django.contrib.auth.models import AbstractUser
class UserInfo(AbstractUser):
    sex_choice = (
        (1, "男"),
        (2, "女")
    )
    role_choice = (
        (1, "admin"),
        (2, "user")
    )
    age = models.DateField(null=True, blank=True, verbose_name="年龄")
    sex = models.IntegerField(choices=sex_choice, default=1, verbose_name="性别")
    role = models.IntegerField(choices=role_choice, default=1, verbose_name="角色")
    mobile = models.CharField(max_length=15, null=True, blank=True, verbose_name="手机号码")
    avatar = models.CharField(max_length=200, null=True, blank=True, verbose_name="用户头像")


    class Meta:
        db_table = "userinfo"
        verbose_name = "userinfo"
        verbose_name_plural = '用户信息'
        ordering = ('-id',)

    def __str__(self):
        return self.username


#记录用户登录日志
class UserLog(models.Model):
    username = models.CharField(max_length=50, verbose_name="账号")
    first_name = models.CharField(max_length=50, verbose_name="姓名")
    remote_ip = models.GenericIPAddressField(verbose_name="登录IP")
    status_choice = (
        (1, "成功"),
        (2, "失败")
    )
    status = models.IntegerField(choices=status_choice, default=1, verbose_name="状态")
    msg = models.TextField(max_length=50,verbose_name="登录日志")
    create_time = models.DateTimeField(auto_now_add=True, verbose_name="登录时间")

    class Meta:
        db_table = "userlog"
        verbose_name = "userlog"
        verbose_name_plural = '用户日志'
        ordering = ('-id',)

    def __str__(self):
        return self.username

from django.utils import timezone
class OperationLog(models.Model):
    username = models.CharField(max_length=20,verbose_name="账号")
    url = models.CharField(max_length=50,verbose_name="请求地址")
    action = models.CharField(max_length=100,verbose_name="日志标题")  # 例如: 'create', 'update', 'delete'
    status_choice = (
        (1, "成功"),
        (2, "失败")
    )
    remote_ip = models.GenericIPAddressField(verbose_name="IP地址")
    status = models.IntegerField(choices=status_choice, default=1, verbose_name="状态")
    timestamp = models.DateTimeField(default=timezone.now,verbose_name="操作时间")

    class Meta:
        db_table = "operationlog"
        verbose_name = "operationlog"
        verbose_name_plural = '操作日志'
        ordering = ('-id',)

    def __str__(self):
        return self.username

